package com.elong.spring.lifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor{

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	if("carlifecycle".equals(beanName)){
	    CarLifeCycle carLifeCycle = (CarLifeCycle)bean;
	    if(carLifeCycle.getColor() == null){
		System.out.println("调用BeanPostProcessor.postProcessBeforeInitialization,"
			+ "color为空，设置为默认黑色");
		carLifeCycle.setColor("黑色");
	    }
	}
	return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
	if("carlifecycle".equals(beanName)){
	    CarLifeCycle carLifeCycle = (CarLifeCycle)bean;
	    if(carLifeCycle.getMaxSpeed() >= 200){
		System.out.println("调用BeanPostProcessor.postProcessAfterInitialization,"
			+ "maxSpeed大于200，调整为200");
		carLifeCycle.setMaxSpeed(200);
	    }
	}
	return bean;
    }

}
